package com.usian.controller;

import com.usian.PageResult;
import com.usian.Result;
import com.usian.feign.ContentFeign;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/content")
public class ContentController {

    @Autowired
    private ContentFeign contentFeign;

    @RequestMapping("selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Long id){
        List<TbContentCategory> categoryList = contentFeign.selectContentCategoryByParentId(id);
        if (categoryList!=null && categoryList.size()>0) {
            return Result.ok(categoryList);
        }
        return Result.error("查询无果");
    }

    /**
     * 添加内容分类
     *
     * @param parentId
     * @param name
     * @return
     */
    @RequestMapping("insertContentCategory")
    public Result insertContentCategory(@RequestParam Long parentId,@RequestParam String name ){
        Integer integer = contentFeign.insertContentCategory(parentId, name);
        if (integer>0){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 删除内容分类
     *
     * @param categoryId
     * @return
     */
    @RequestMapping("deleteContentCategoryById")
    public Result deleteContentCategoryById(@RequestParam Long categoryId){
        Integer integer = contentFeign.deleteContentCategoryById(categoryId);
        if (integer==200) {
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 修改内容分类
     *
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("updateContentCategory")
    public Result updateContentCategory(@RequestParam Long id,@RequestParam String name){
        Integer integer = contentFeign.updateContentCategory(id, name);
        if (integer>0){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    @RequestMapping("selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(@RequestParam Long categoryId,
                                                     @RequestParam(defaultValue = "1") Integer page,
                                                     @RequestParam(defaultValue = "10") Integer rows){
        PageResult pageResult = contentFeign.selectTbContentAllByCategoryId(categoryId, page, rows);
        if (pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return Result.error("查询无果");
    }

    /**
     * 增加分类内容
     *
     * @param tbContent
     * @return
     */
    @RequestMapping("insertTbContent")
    public Result insertTbContent(TbContent tbContent){
        Integer integer = contentFeign.insertTbContent(tbContent);
        if (integer>0) {
         return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    @RequestMapping("deleteContentByIds")
    public Result deleteContentByIds(@RequestParam Long ids){
        Integer integer = contentFeign.deleteContentByIds(ids);
        if (integer>0){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
}
